<?php
//------------------------
// 自动生成代码
//-------------------------

namespace app\console\controller;

use app\console\model\Privilege as SelfModel;
use think\facade\Config;

use think\facade\Db;
use think\facade\View;
@ini_set('display_errors','Off');
class Generate extends Base
{
    /**
     * 首页
     * @return mixed
     */
    public function index()
    {


        $tables = Db::getTables();

        View::assign('tables', $tables);

        if ($this->request->param('table')) {
            $table = $this->request->param('table');
            $prefix = Config::get('database.connections.mysql.prefix');
            $tableInfo = Db::getTableInfo($table);

            $controller = parse_name(preg_replace('/^(' . $prefix . ')/', '', $table), 1);
            View::assign('table_info', json_encode($tableInfo));
            View::assign('controller', $controller);
        }
        $where = "id > 90 and (url like '%index%' or parent_id = 0)";
        $data = SelfModel::order(['sort' => 'asc','id' => 'asc'])->where($where)->select()->toArray();
        $data = getMenuTree($data);

        View::assign('privilege', $data);
        return View::fetch('./plugin/generate/index.html');
    }

    /**
     * 生成代码
     */

    public function run()
    {
        $generate = new \Generate();

        $data = $this->request->post();
        if(empty($data['pri_name'])) fail('请填写权限模块名称');
        if(empty($data['title'])) fail('请选择权限菜单');
        $generate->run($data);
        win('生成成功，去看看', $data['module'].'/'.$data['controller'].'/index');
    }
}
